<template>
  <div class="update-gender">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import datjs from 'dayjs'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1950, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })
      // const data = `${this.currentDate.getFullYear()}-${this.currentDate
      // .getMonth()+1}-${this.currentDate.getDate()+1}`
      // 后端需要的数据格式
      const date = datjs(this.currentDate).format('YYYY-MM-DD')
      try {
        await updateUserProfile({
          birthday: date
        })
        this.$emit('close')
        this.$emit('input', date)
      } catch (error) {
        console.log(error)
      }
      this.$toast.success('保存成功')
    },
    onGenderChange (pinker, value, index) {
      this.defaultIndex = index
    }
  }
}
</script>

<style lang="less" scoped>
</style>
